# CodeName：MainWindows
# Authon：liutao
# E-Mail：wxyj3496@126.com
# Creat time：2021-08-22
# Last Modified by:liutao
# Last Modified Time:2021-09-26
# futction：实现人机交互的窗口,调用拆分代码


import tkinter as tk
from tkinter import ttk
import tkinter.filedialog as tkfd
import Split_MX as spmx
from tkinter import messagebox as msg
import threading


class MainWindow():

	def __init__(self):
		self.win = tk.Tk()
		self.win.title('SplitList_513')
		self.win.resizable(False, False)
		self.create_widgets()

	# 窗口退出函数
	def _quit(self):
		self.win.quit()
		self.win.destroy()
		exit()

	# 定义Help-About窗口的信息

	def _about(self):
		win_about = tk.Toplevel()
		win_about.title('About SplitList_513')
		about_name = tk.Label(win_about, text='SplitList_513')
		about_name.config(font=('Arial', 30), foreground='red', width=12)
		about_name.config(pady=5)
		about_name.pack()

		tk.Label(win_about, text='Designed by LiuTao @2021', pady=3).pack()
		tk.Label(win_about, text='E-mail: wxyj3496@126.com', pady=3).pack()
		tk.Label(win_about, text='Telephone: 13356981975', pady=3).pack()
		tk.Label(win_about, text='Version: D1.00', pady=3).pack()

	# 浏览选取源文件路径的函数，限制可见文件格式为excel的xls和xlsx
	def getsourcefilepath(self):
		filename = tkfd.askopenfilename(title='选择CMS数据源文件',\
			filetypes=[('excel file','*.xlsx'),('excel file','*.xls')])
		self.path_sourcefileVar.set(filename)

	# 浏览选取输入文件路径的函数
	def getinputfilepath(self):
		filename = tkfd.askopenfilename(title='选择明细表',\
			filetypes=[('excel file','*.xlsx'),('excel file','*.xls')])
		self.path_inputfileVar.set(filename)

	# 浏览选取BOM文件路径的函数
	def getBOMfilepath(self):
		filename = tkfd.askopenfilename(title='选择BOM文件模板',\
			filetypes=[('excel file','*.xlsx'),('excel file','*.xls')])
		self.path_BOMfileVar.set(filename)

	# 浏览选取文件夹路径的函数
	def getoutputfoderpath(self):
		foderpath = tkfd.askdirectory(title='选择输出文件目录')
		self.path_outputforderVar.set(foderpath)


	# 清空三个信息框的内容
	def path_clear(self):
		self.path_sourcefileVar.set('')
		self.path_inputfileVar.set('')
		self.path_BOMfileVar.set('')
		self.path_outputforderVar.set('')

	#thread 一个线程
	def thread_it(self, func, *args):
		#创建
		t = threading.Thread(target=func, args=args)
		t.setDaemon(True)
		t.start()





	# 开始拆分表格，调用拆分代码
	def startsplit(self):
		path_sourcefile = self.path_sourcefileVar.get()
		path_inputfile = self.path_inputfileVar.get()
		path_BOMfile = self.path_BOMfileVar.get()
		path_outputforder = self.path_outputforderVar.get()
		if path_sourcefile == '' or path_inputfile == '' or path_BOMfile == '' or path_outputforder == '':
			msg.showinfo('错误提示', '文件路径不全，请选择！')
		elif path_sourcefile == path_inputfile:
			msg.showinfo('错误提示', '库文件和明细表指定文件相同，请重新选择！')
		elif path_BOMfile == path_sourcefile:
			msg.showinfo('错误提示', '库文件和BOM表指定文件相同，请重新选择！')
		elif path_BOMfile == path_inputfile:
			msg.showinfo('错误提示', '明细表和BOM表指定文件相同，请重新选择！')
		else:
			# spmx.Split_MX(path_sourcefile, path_inputfile, path_BOMfile, path_outputforder, self.progress_bar)
			self.thread_it(spmx.Split_MX,path_sourcefile, path_inputfile, path_BOMfile, path_outputforder, self.progress_bar)

	# 创建主窗口下各个组件
	def create_widgets(self):
		# =================Menu==========================================
		# 定义菜单栏，'File'和'Help'
		menu_bar = tk.Menu(self.win)
		self.win.config(menu=menu_bar)

		file_menu = tk.Menu(menu_bar, tearoff=0)
		menu_bar.add_cascade(label='File', menu=file_menu)
		file_menu.add_command(label='Exit', command=self._quit)

		help_menu = tk.Menu(menu_bar, tearoff=0)
		menu_bar.add_cascade(label='Help', menu=help_menu)
		help_menu.add_command(label='About SplitList_513', command=self._about)

		# =================Label========================================
		# 定义四个标签
		label_source = tk.Label(self.win,text='CMS数据文件').grid(row=0,\
			column=0, padx=5, sticky='W')
		label_input = tk.Label(self.win,text='明细表文件').grid(row=1,\
			column=0, padx=5, sticky='W')
		label_BOM = tk.Label(self.win,text='BOM文件格式').grid(row=2,\
			column=0, padx=5, sticky='W')
		label_output = tk.Label(self.win,text='文件输出路径').grid(row=3,\
			column=0, padx=5, sticky='W')

		# ================Entry========================================
		#定义获取路径的四个变量
		self.path_sourcefileVar = tk.StringVar()
		self.path_inputfileVar = tk.StringVar()
		self.path_BOMfileVar = tk.StringVar()
		self.path_outputforderVar = tk.StringVar()

		#定义四个显示路径的文本框，设置为只读
		entry_source = ttk.Entry(self.win, width=50,textvariable = self.path_sourcefileVar,\
			state='readonly')
		entry_source.grid(row=0, column=1,sticky='W', padx=5)
		entry_input = ttk.Entry(self.win, width=50,textvariable = self.path_inputfileVar,\
			state='readonly')
		entry_input.grid(row=1, column=1,sticky='W', padx=5)
		entry_BOM = ttk.Entry(self.win, width=50,textvariable = self.path_BOMfileVar,\
			state='readonly')
		entry_BOM.grid(row=2, column=1,sticky='W', padx=5)
		entry_output = ttk.Entry(self.win, width=50,textvariable = self.path_outputforderVar,\
			state='readonly')
		entry_output.grid(row=3, column=1,sticky='W', padx=5)

		# ================Button========================================
		#定义5个执行按钮
		button_source = ttk.Button(self.win, text = '浏览...', command = self.getsourcefilepath\
			).grid(row=0, column=2,sticky='W', padx=5)
		button_input = ttk.Button(self.win, text = '浏览...', command = self.getinputfilepath\
			).grid(row=1, column=2,sticky='W', padx=5)
		button_BOM = ttk.Button(self.win, text = '浏览...', command = self.getBOMfilepath\
			).grid(row=2, column=2,sticky='W', padx=5)
		button_output = ttk.Button(self.win, text = '浏览...', command = self.getoutputfoderpath\
			).grid(row=3, column=2,sticky='W', padx=5)
		button_split = ttk.Button(self.win, text = '表格拆分', command = self.startsplit\
			).grid(row=4, column=2,sticky='W', padx=5, pady =5)
		button_split = ttk.Button(self.win, text = '清空路径', command = self.path_clear\
			).grid(row=4, column=0,sticky='W', padx=5, pady =5)

		#===============进度条===========================================
		self.progress_bar = ttk.Progressbar(self.win, orient = 'horizontal',length = 550,\
			mode = 'determinate')
		self.progress_bar.grid(row=5, column=0,sticky='W', padx=5, pady =5, columnspan=3)
		self.progress_bar['maximum'] = 100


# =====================================================================
win_main = MainWindow()
win_main.win.mainloop()